package com.qingxin.sys.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.entity.SysPermission;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.SysPermissionService;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.utils.StringUtils;

/**
 * 权限管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/perm")
public class PermissionAction extends BaseAction {

	@Autowired
	private SysPermissionService sysPermissionService;
	
	
	@RequestMapping(value="/permJstree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object permJstree(String pid){
		if(StringUtils.isEmpty(pid) || "#".equals(pid)){
			List<JsTreeNode> list = new ArrayList<>();
			list.add(new JsTreeNode("0", "权限权限", "PERM", "perid", true));
			return list;
		}else{
			List<JsTreeNode> list = sysPermissionService.getMenuData(pid);
			return list;
		}
	}
	
	
	/**
	 * 加载树形数据
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/permtree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object permtree(String pid){
		if(StringUtils.isEmpty(pid)){
			List<JsTreeNode> list = new ArrayList<JsTreeNode>();
			list.add(new JsTreeNode("0", "权限权限", "PERM", "perid", true));
			return list;
		}else{
			return sysPermissionService.getMenuData(pid);
		}
	}
	
	/**
	 * 列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "sys/perm/perm-list";
	}
	
	
	@RequestMapping(value="/list2.do", method=RequestMethod.GET)
	public String list2(ModelMap model, HttpServletRequest request){
		return "sys/perm/perm-list2";
	}
	
	/**
	 * 列表数据
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(@RequestParam(value="leftdata")String pid){
		return sysPermissionService.listData(pid);
	}
	
	
	
	/**
	 * 添加
	 * @param model
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, @RequestParam(value="leftdata")String pid){
		model.put("pid", pid);
		
		SysPermission permission = sysPermissionService.get(pid);
		if(permission != null){
			model.put("pcode", permission.getPer_code());
		}
		
		model.put("newid", sysPermissionService.getNewID(pid));
		model.put("newindex", sysPermissionService.getNewPerindex(pid));
		
		return "sys/perm/perm-add";
	}
	
	/**
	 * 保存
	 * @param permission
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid SysPermission permission, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return sysPermissionService.add(permission);
	}
	
	/**
	 * 修改
	 * @param model
	 * @param perid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String perid){
		SysPermission permission = sysPermissionService.get(perid);
		model.put("perm", permission);
		return "sys/perm/perm-edit";
	}
	
	/**
	 * 保存修改
	 * @param permission
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid SysPermission permission, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return sysPermissionService.update(permission);
	}
	
	/**
	 * 删除
	 * @param perid
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] perid){
		if(perid==null || perid.length==0){
			return new SysError("没有选择任何数据.");
		}
		return sysPermissionService.delete(perid);
	}
	
	/**
	 * 判断ID是否被使用
	 * 
	 * @param perid
	 * @return 0未被使用、1已被使用
	 */
	@RequestMapping(value="/isPeridDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isPeridDuplicate(String perid){
		boolean existed = sysPermissionService.isPeridDuplicate(perid);
		return existed ? "1":"0";
	}
	
	/**
	 * 判断编号是否被使用
	 * 
	 * @param menucode
	 * @param menuid
	 * @return 0未被使用、1已被使用
	 */
	@RequestMapping(value="/isPercodeDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isPercodeDuplicate(String per_code, String perid){
		boolean existed = sysPermissionService.isPercodeDuplicate(per_code, perid);
		return existed ? "1":"0";
	}
	
	
	
}
